package com.jiayang.hhotel.presenter;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.NowLiveSearchDialogAdapter;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.NowLiveSearchModel;
import com.jiayang.hhotel.listener.OnItemClickListener;
import com.jiayang.hhotel.net.Api;
import com.jiayang.hhotel.ui.NowLiveSearchActivity;
import com.jiayang.hhotel.util.LogUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by Administrator on 2017/5/24 0024.
 *现住查询逻辑类
 */

public class NowLiveSearchPresenter extends XPresent<NowLiveSearchActivity> {

    /**
     * 请求现住列表
     * @param mpLanguage 语言
     * @param mpOccdate_b 开始时间
     * @param mpOccdate_e 结束时间
     * @param mpGsttype --：0-全部、1-散、2-团会、3-旅行社、4-长包房
     * @param mpPickup --接送：0-全部、1-到离店接送
     * @param mpIsvip --VIP：0-全部、1-是
     * @param mpIsed --明日离店：0-全部、1-是
     * @param mpIsSecret --保密：0-全部、1-是
     * @param mpIsmem --会员：0-全部、1-是
     * @param mpGstname 客人名字
     * @param mpRoom 房号
     * @param mpIsforeign --：0-全部、1-外宾、2-内宾
     * @param mpIsbirthday --店内生日：0-全部、1-是
     * @param mpIshis --历史客人：0-全部、1-是
     * @param mpIsfree --免费：0-全部、1-是
     * @param mpIsupgrade --升级：0-全部、1-是
     * @param mpIsdaily --一日租：0-全部、1-是
     * @param mpRenttype --出租方式：0-全部、1-钟点、2-床租、3-拼房
     * @param mpIsover --即将超限额：0-全部、1-是
     * @param mpIswalkin --上门：0-全部、1-是
     * @param page 列表请求页数   请求的次数
     */
        public void asyncLoad(int mpLanguage,String mpOccdate_b,String mpOccdate_e,int mpGsttype,int mpPickup,
                              int mpIsvip,int mpIsed,int mpIsSecret,int mpIsmem,String mpGstname,String mpRoom,
                              int mpIsforeign,int mpIsbirthday,int mpIshis,int mpIsfree,int mpIsupgrade,int mpIsdaily,
                              int mpRenttype,int mpIsover,int mpIswalkin,String mpRmType,int page){
            Api.getHomeSer().getNowLiveList(Constant.PHONE,mpLanguage,0,1,mpOccdate_b,mpOccdate_e
                    ,2,mpGsttype,mpPickup,mpIsvip,mpIsed,mpIsSecret,mpIsmem,mpGstname,mpRoom
                    ,mpIsforeign,mpIsbirthday,mpIshis,mpIsfree,mpIsupgrade,mpIsdaily,mpRenttype,mpIsover
                    ,mpIswalkin,mpRmType,page)
                    .compose(XApi.<NowLiveSearchModel>getApiTransformer())
                    .compose(XApi.<NowLiveSearchModel>getScheduler())
                    .compose(getV().<NowLiveSearchModel>bindToLifecycle())
                    .subscribe(new ApiSubscriber<NowLiveSearchModel>() {
                        @Override
                        protected void onFail(NetError error) {
                            isSuccess = false;
                            currentIndex = -1;
                            getV().sealPic(error.getType());
                        }

                        @Override
                        public void onNext(NowLiveSearchModel results) {
                            isSuccess = false;
                            LogUtil.showLog("results="+results.toString());
                            switch (results.getState()){
                                case 9://成功
                                    getV().setAdapter(results.getPreceptionListDto());
                                    break;
                                case -1://失败
                                    currentIndex = -1;
                                    getV().sealPic(-1);
                                    break;
                                case 1002://没有数据
                                    getV().sealPic(1002);
                                    break;
                            }
                        }
                    });
        }

    private AlertDialog searchDialog;
    private int currentIndex = -1;//上次选中的位置下标
    private boolean isSuccess;//是否加载完成
    /**
     * 创建现住查询标题右侧按钮的菜单对话框
     * @param context 上下文
     */
    public void createSearchDialog(final Context context){
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View itemView = LayoutInflater.from(context).inflate(R.layout.dialog_now_live_search,null);
        GridView view = (GridView) itemView.findViewById(R.id.gv);
        final List<String> list = new ArrayList<>(Arrays.asList(context.getResources().getStringArray(R.array.now_live_search_list)));
        NowLiveSearchDialogAdapter adapter = new NowLiveSearchDialogAdapter(context, list, currentIndex,new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                searchDialog.dismiss();
                currentIndex = position;
                if(!isSuccess){
                    isSuccess = true;
                    switch (position){
                        case 0://内宾
                            getV().initRequest(0,0,0,0,0,0,2,0,0,0,0,0,0,0,0);
                            break;
                        case 1://外宾
                            getV().initRequest(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0);
                            break;
                        case 2://散客
                            getV().initRequest(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
                            break;
                        case 3://团会
                            getV().initRequest(2,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
                            break;
                        case 4://旅行社
                            getV().initRequest(3,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
                            break;
                        case 5://明日离店
                            getV().initRequest(0,0,0,1,0,0,0,0,0,0,0,0,0,0,0);
                            break;
                        case 6://公会(没有此字段)
                            getV().initRequest(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
                            break;
                        case 7://VIP
                            getV().initRequest(0,0,1,0,0,0,0,0,0,0,0,0,0,0,0);
                            break;
                        case 8://保密
                            getV().initRequest(0,0,0,0,1,0,0,0,0,0,0,0,0,0,0);
                            break;
                        case 9://店内生日
                            getV().initRequest(0,0,0,0,0,0,0,0,0,0,0,1,0,0,0);
                            break;
                        case 10://回头客
                            getV().initRequest(0,0,0,0,0,0,0,0,1,0,0,0,0,0,0);
                            break;
                        case 11://免费
                            getV().initRequest(0,0,0,0,0,0,0,0,0,1,0,0,0,0,0);
                            break;
                        case 12://升级
                            getV().initRequest(0,0,0,0,0,0,0,0,0,0,1,0,0,0,0);
                            break;
                        case 13://旧租
                            getV().initRequest(0,0,0,0,0,0,0,0,0,0,0,1,0,0,0);
                            break;
                        case 14://钟点房
                            getV().initRequest(0,0,0,0,0,0,0,0,0,0,0,0,1,0,0);
                            break;
                        case 15://拼房
                            getV().initRequest(0,0,0,0,0,0,0,0,0,0,0,0,3,0,0);
                            break;
                        case 16://床租
                            getV().initRequest(0,0,0,0,0,0,0,0,0,0,0,0,2,0,0);
                            break;
                        case 17://送机
                            getV().initRequest(0,1,0,0,0,0,0,0,0,0,0,0,0,0,0);
                            break;
                        case 18://即将起额
                            getV().initRequest(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0);
                            break;
                    }
                }
            }
        });
        view.setAdapter(adapter);
        builder.setView(itemView);
        searchDialog = builder.show();
    }
}
